//
//  SDLoadingView.m
//  LoadingAnimation
//
//  Created by 栋栋 施 on 15/8/18.
//  Copyright (c) 2015年 栋栋 施. All rights reserved.
//

#import "SDLoadingView.h"

static const CGFloat IMGCircleLineWidth = 1.65;

CGPoint CGRectGetCenter(CGRect rect)
{
    return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}

@interface SDLoadingView()
{
    
}
@property (nonatomic, strong) NSArray *strokeTimings;
@property (nonatomic, strong) CADisplayLink *timer;
@property (nonatomic, strong) CAShapeLayer *circleLayer;
@end

@implementation SDLoadingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _strokeColor = [UIColor whiteColor];
        [self createLayers];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        _strokeColor = [UIColor whiteColor];
        [self createLayers];
    }
    return self;
}

- (void)createLayers
{
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 ,CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
  //  backgroundView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    backgroundView.layer.borderColor = [UIColor blackColor].CGColor;
    backgroundView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1];
    
    _circleLayer = [CAShapeLayer layer];
    CGFloat radius = 16.0;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:backgroundView.center
                                                        radius:radius
                                                    startAngle:-0.5 * M_PI
                                                      endAngle:1.5 * M_PI
                                                     clockwise:YES];
    
    _circleLayer.path = path.CGPath;
    _circleLayer.strokeColor = self.strokeColor.CGColor;
    _circleLayer.lineWidth = IMGCircleLineWidth;
    _circleLayer.fillColor = nil;
    _circleLayer.strokeStart = 0.0;
    _circleLayer.strokeEnd = 0.0;
    _circleLayer.contentsScale = [UIScreen mainScreen].scale;
    
    [backgroundView.layer addSublayer:_circleLayer];
    [self addSubview:backgroundView];
    //[self loopAnimations];
    

    self.timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(loopAnimations)];
    self.timer.frameInterval = 60 *  1.4;
    [self.timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)loopAnimations
{
    CGFloat timeDuration =  0.5;
    
    CABasicAnimation *strokEndAnimation  = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    strokEndAnimation.fromValue = @0;
    strokEndAnimation.toValue = @1.08;
    strokEndAnimation.duration = 1.4;
    strokEndAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    strokEndAnimation.beginTime = CACurrentMediaTime() + timeDuration;
    [_circleLayer addAnimation:strokEndAnimation forKey:nil];
    
    CABasicAnimation *strokeStartAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    strokeStartAnimation.fromValue = @0;
    strokeStartAnimation.toValue = @1.08;
    strokeStartAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    strokeStartAnimation.beginTime = CACurrentMediaTime() + 2 * timeDuration;
    strokeStartAnimation.duration = 1.4 - timeDuration;
    [_circleLayer addAnimation:strokeStartAnimation forKey:nil];
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
